
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;

public class baicizhan extends JFrame implements ActionListener {
    // 所有的单词
    ArrayList<String> totalWords = new ArrayList<String>();
    // 用来测试的单词
    ArrayList<String> testWords = new ArrayList<String>();
    JLabel scoreLabel = new JLabel(); // 答对单词数
    JLabel wordLabel = new JLabel(); // 正在测试的单词
    ButtonGroup group = new ButtonGroup();// 单选按钮组
    JRadioButton rb1 = new JRadioButton();// 选项一单选按钮
    JRadioButton rb2 = new JRadioButton();// 选项二单选按钮
    JRadioButton rb3 = new JRadioButton();// 选项三单选按钮
    JRadioButton rb4 = new JRadioButton();// 选项四单选按钮
    JLabel answerLabel = new JLabel(); // 显示正确答案标签
    JButton continueButton = new JButton("继续");
    JButton endButton = new JButton("结束");
    // 英语单词和正确答案
    String Englishword, answer;
    String item[] = new String[4];
    int totalNum;// 测试的单词总数
    int okNum = 0; // 答对的单词数
    int index; // 当前的单词在测试单词表中的序号

    baicizhan() {
        this.setTitle("百词斩");
        this.setSize(500, 600);
        this.setLayout(null);
// 设置答对的单词数
        scoreLabel.setForeground(Color.red);// 设置颜色
        Font font = new Font("宋体", Font.PLAIN, 24);
        scoreLabel.setFont(font); // 设置字体
        scoreLabel.setBounds(200, 20, 300, 30);
// 设置正在测试的英语单词的字体
        wordLabel.setFont(font);
        wordLabel.setBounds(40, 80, 200, 30);
        rb1.setFont(font);// 设置选项一的字体字号
        rb2.setFont(font);// 设置选项二的字体字号
        rb3.setFont(font);// 设置选项三的字体字号
        rb4.setFont(font);// 设置选项四的字体字号
        rb1.setBounds(40, 120, 350, 50);
        rb2.setBounds(40, 170, 350, 50);
        rb3.setBounds(40, 220, 350, 50);
        rb4.setBounds(40, 270, 350, 50);
// 添加动作命令标识
        rb1.setActionCommand("1");
        rb2.setActionCommand("2");
        rb3.setActionCommand("3");
        rb4.setActionCommand("4");
// 将各个单选按钮加入到按钮组，确保多选一（一个按钮组内只能选一项）
        group.add(rb1);
        group.add(rb2);
        group.add(rb3);
        group.add(rb4);
// 四个选项按钮添加监听
        rb1.addActionListener(this);
        rb2.addActionListener(this);
        rb3.addActionListener(this);
        rb4.addActionListener(this);
// 设置答案标签
        answerLabel.setBounds(50, 350, 350, 50);
        answerLabel.setFont(font);
        answerLabel.setForeground(Color.red);// 设置答案的文字颜色
// 设置继续按钮
        continueButton.setFont(font);
        continueButton.setBounds(100, 450, 120, 30);
        continueButton.setActionCommand("continue");
        continueButton.addActionListener(this);// 添加监听
// 所有的单词都掌握后，不能再继续
        continueButton.setEnabled(true);
// 设置结束按钮
        endButton.setFont(font);
        endButton.setBounds(250, 450, 120, 30);
        endButton.setActionCommand("end");
        endButton.addActionListener(this); // 添加监听
// 将个组件添加到窗口中
        this.add(scoreLabel);
        this.add(wordLabel);
        this.add(rb1);
        this.add(rb2);
        this.add(rb3);
        this.add(rb4);
        this.add(continueButton);
        this.add(endButton);
        this.add(answerLabel);
        this.setVisible(true); // 设置窗口可见
        this.setLocationRelativeTo(null);// 设置窗口位置
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        char operation = cmd.charAt(0);
        String choice = "";
        if (operation >= '1' && operation <= '4') {
            switch (operation) {
                case '1':
                    choice = rb1.getText();
                    break;
                case '2':
                    choice = rb2.getText();
                    break;
                case '3':
                    choice = rb3.getText();
                    break;
                case '4':
                    choice = rb4.getText();
                    break;
            }
            if (choice.equals(answer)) {
                answerLabel.setText("√");
                testWords.remove(index);
                okNum = testWords.size();
                scoreLabel.setText("答对的单词数：" + (totalNum - okNum) + "/" + totalNum);
                if (okNum == 0)
                    continueButton.setEnabled(false);
            } else
                answerLabel.setText("× " + answer);
            rb1.setEnabled(false);
            rb2.setEnabled(false);
            rb3.setEnabled(false);
            rb4.setEnabled(false);
        }
        if (cmd.equals("continue")) {
            group.clearSelection();
            answerLabel.setText("");
            select_Word();
        }
        if (cmd.equals("end"))
            System.exit(0);
    }

    void select_Word() {
        Random random = new Random();
// 总单词表
        int totalWordsNum = totalWords.size();
// 使用的单词表
        int testWordsNum = testWords.size();
// 随机选取一个单词
        index = random.nextInt(testWordsNum);
        String str = testWords.get(index);
// 分离出英语单词和答案
        String s[] = str.split(" ");
        Englishword = s[0];
        answer = s[1];
        int i, j, k, answerIdx;
// 随机设置正确选项的序号
        answerIdx = random.nextInt(4);
// 设置正确选项
        item[answerIdx] = answer;
// 设置其它三个错误选项
        for (i = 0; i < 4; i++) {
            if (i == answerIdx) // 跳过正确选项
                continue;
// 从总单词表中随机读取一个单词的中文解释作为错误选项
            j = random.nextInt(totalWordsNum);
            for (; ; j = (j + 1) % totalWordsNum) {
                str = totalWords.get(j);
                s = str.split(" ");
                String errItem = s[1];
// 如果和正确选项相同，取下一个单词
                if (errItem.equals(answer))
                    continue;
                for (k = 0; k < i; k++)
                    if (errItem.equals(item[k]))
                        break;
// 如果和前面的选项相同，取下一个单词
                if (k < i)
                    continue;
// 设置错误选项
                item[i] = errItem;
                break;
            }
        }
// 设置英文单词和四个选项
        wordLabel.setText(Englishword);
        rb1.setText(item[0]);// 设置选项一的文字
        rb2.setText(item[1]);// 设置选项二的文字
        rb3.setText(item[2]);// 设置选项三的文字
        rb4.setText(item[3]);// 设置选项四的文字
        rb1.setEnabled(true);
        rb2.setEnabled(true);
        rb3.setEnabled(true);
        rb4.setEnabled(true);
    }

    public static void main(String[] args) throws IOException {
        baicizhan win = new baicizhan();
        FileReader in = new FileReader("C:\\code\\java\\text\\src\\demo.txt");
        BufferedReader bin = new BufferedReader(in);
        String s;
// 从单词库文件中，读取所有的单词存入到总单词表totalWords中
        while ((s = bin.readLine()) != null) {
            win.totalWords.add(s);
        }
        bin.close();
        in.close();
// 从总单词表totalWords中随机读取100个单词存入到代词表testWords中
        int totalWordsNum = win.totalWords.size();
        Random random = new Random();
        win.totalNum = 10;
        for (int i = 0; i < win.totalNum; i++) {
            int index = random.nextInt(totalWordsNum);
            s = win.totalWords.get(index);
            win.testWords.add(s);
            win.totalWords.remove(index);
            totalWordsNum--;
        }
        win.scoreLabel.setText("答对的单词数：" + 0 + "/" + win.totalNum);
        win.select_Word();
    }
}